const { connection } = require('../connect/connect')
var url = require('url');
var util = require('util');


exports.getCart = function (req, res){
    let params = url.parse(req.url,true).query
    let usernameId =params.usernameId;
    let book_id = params.book_id;
    let sql = '';
    if(book_id !== undefined){
        sql = 'select * from cart where user_id='+usernameId+' and book_id='+book_id;
    }else {
        sql = 'select * from cart where user_id=' + usernameId;
    }
    connection.query(sql,function(err,result){
        if(err){
            res.send(err);
        }else{
            res.send(result);
        }
    })
}

exports.addCart = function (req, res){
    console.log('*******')
    let params = url.parse(req.url, true).query;
    let usernameId = params.usernameId;
    let book_id = params.book_id;
    let book_num = params.book_num;
    let book_img = params.book_img;
    let book_name = params.book_name;
    let book_sum = params.book_sum;
    let sql = 'insert into cart(user_id,book_id,book_num,book_img,book_name,book_sum) values(?,?,?,?,?,?)';
    let paramsData = [usernameId,book_id,book_num,book_img,book_name,book_sum]
    connection.query(sql,paramsData,function(err,result){
        if(err){
            res.send(err)
        }else{
            res.send(true)
        }
    })
}

exports.addOrder = function(req,res){
    let params = url.parse(req.url,true).query;
    let book_id = params.book_id;
    let usernameId= params.usernameId;
    let book_num = params.book_num;
    let book_sum = params.book_sum;
    let book_name = params.book_name;
    let book_img = params.book_img;
    let sql = 'insert into jerryOrder(user_id,book_id,book_num,book_sum,book_name,book_img) values(?,?,?,?,?,?)';
    let paramsData = [usernameId,book_id,book_num,book_sum,book_name,book_img];
    connection.query(sql,paramsData,function(err,result){
        if(err){
            res.send(err)
        }else{
            res.send(result)
        }
    })
}
exports.getOrder = function(req,res){
    let params = url.parse(req.url,true).query;
    let usernameId = params.usernameId;
    connection.query('select * from jerryOrder where user_id='+usernameId,function(err,result){
        if(err){
            res.send(err);
        }else {
            res.send(result)
        }
    })
}
exports.getOrderDetail = function(req,res){
    let params = url.parse(req.url,true).query;
    let id = params.id;
    let sql = 'select * from jerryOrder where id='+id;
    connection.query(sql,function(err,result){
        if(err){
            res.send(err)
        }else{
            res.send(result)
        }
    })
}
exports.deleteOrder  = function(req,res){
    let params = url.parse(req.url,true).query;
    let id = params.id;
    let sql = 'delete  from jerryOrder where id='+id;
    connection.query(sql,function(err,result){
        if(err){
            res.send(err)
        }else{
            res.send(result)
        }
    })
}

exports.deleteCart = function(req,res){
    let params = url.parse(req.url,true).query;
    let id = params.id;
    console.log(id)
    let sql = 'delete from cart where id='+id;
    connection.query(sql,function(err,result){
        if(err){
            res.send(err)
        }else{
            res.send(result)
        }
    })
}

